package com.lihan.GetSeatInLib.Controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lihan.GetSeatInLib.Domain.LibTable;
import com.lihan.GetSeatInLib.Domain.SeatModel;
import com.lihan.GetSeatInLib.Domain.StandReturnMessage;
import com.lihan.GetSeatInLib.Domain.Enums.SeatStaus;
import com.lihan.GetSeatInLib.Service.LibTableService;
import com.lihan.GetSeatInLib.Service.SeatService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "桌位操作接口")
@RestController
@RequestMapping("/Seat")
public class SeatController {
	
	@Autowired
	private SeatService seatService;
	
	@Autowired
	private LibTableService libTableService;
	
	/**
	 * 
	 * @Title: CreateSeat
	 * @Description: 在一个已存在的桌子上上创建座位
	 * @param @param belongTableId
	 * @param @param SeatIndex
	 * @param @return 参数
	 * @return StandReturnMessage<Boolean> 包装类型
	 * @throws
	 */
	@ApiOperation("创建一个座位")
	@GetMapping("/CreaterSeat")
	public synchronized StandReturnMessage<Boolean> CreateSeat(Long belongTableId,String SeatIndex) {
		
		//先查出所属的桌子，然后再构造SeatModel,
		LibTable belong = libTableService.getTibTableById(belongTableId);
		
		SeatModel newone = new SeatModel();
		
		newone.setIndexNum(SeatIndex);
		newone.setStatus(SeatStaus.LOCKED);  //刚创建的时候先设置为上锁
		newone.setBelong(belong);
		
		//调业务类将其保存起来
		
		boolean result =  seatService.CreatNewSeat(newone);
		if(result)
			return new StandReturnMessage<Boolean>(200, result, 1);
		else
			return new StandReturnMessage<Boolean>(400, result, 0,"新增出错");
	}
	
	/**
	 * 
	 * @Title: LockSeat
	 * @Description: 锁定一个座位
	 * @param @param ID
	 * @param @return 参数
	 * @return Boolean 返回类型
	 * @throws
	 */
	@ApiOperation("锁定一个座位")
	@GetMapping("/LockSeat")
	public StandReturnMessage<Boolean> LockSeat(Long ID) {
		
		boolean result = seatService.LockSeat(ID);
		if(result)
			return new StandReturnMessage<Boolean>(200, result, 1);
		else
			return new StandReturnMessage<Boolean>(400, result, 0,"新增出错");
	}
	
	@ApiOperation("解锁一个座位")
	@GetMapping("/UnLockSeat")
	public StandReturnMessage<Boolean> UnLockSeat(Long ID) {
		boolean result =   seatService.UnLockSeat(ID);
		if(result)
			return new StandReturnMessage<Boolean>(200, result, 1);
		else
			return new StandReturnMessage<Boolean>(400, result, 0,"新增出错");
	}
	
	@ApiOperation("删除一个座位")
	@GetMapping("/DeleteSeat")
	public StandReturnMessage<Boolean> DeleteSeat(Long ID) {
		boolean result = seatService.DeleteById(ID);
		if(result)
			return new StandReturnMessage<Boolean>(200, result, 1);
		else
			return new StandReturnMessage<Boolean>(400, result, 0,"新增出错");
	}

	
	
//	public StandReturnMessage<T> UpdateSeat(Long ID) {
//	 不提供修改座子的API，因为属性太少	
//	}
}
